package lookie.swing.laf;

import java.util.HashSet;
import java.util.Set;

public class LookAndFeelRegistry {

	private Set<LookAndFeelDescriptor> descriptors = new HashSet<LookAndFeelDescriptor>();

	public void addDescriptor(LookAndFeelDescriptor descriptor) {
		descriptors.add(descriptor);
	}

	public void removeDescriptor(LookAndFeelDescriptor descriptor) {
		descriptors.remove(descriptor);
	}

	public void contribute(LookAndFeelDescriptorContributor contributor) {
		contributor.contribute(this);
	}

	public LookAndFeelDescriptor[] getDescriptors() {
		return descriptors
				.toArray(new LookAndFeelDescriptor[descriptors.size()]);
	}

	public LookAndFeelDescriptor[] getDescriptors(String groupId) {
		Set<LookAndFeelDescriptor> set = new HashSet<LookAndFeelDescriptor>();
		for (LookAndFeelDescriptor d : descriptors) {
			if (d.getGroupId().equals(groupId)) {
				set.add(d);
			}
		}
		return set.toArray(new LookAndFeelDescriptor[set.size()]);
	}

	public LookAndFeelDescriptor getDescriptor(String id) {
		for (LookAndFeelDescriptor d : descriptors) {
			if (d.getId().equals(id)) {
				return d;
			}
		}
		return null;
	}

	public String[] getGroupIds() {
		Set<String> set = new HashSet<String>();
		for (LookAndFeelDescriptor d : descriptors) {
			String groupId = d.getGroupId();
			if (!set.contains(groupId)) {
				set.add(groupId);
			}
		}
		return set.toArray(new String[set.size()]);
	}

}
